package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import lombok.Getter;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * Created by maowenjie on 2017/4/17.
 *
 */
@Data
public class DrugPurchaseRecord implements Serializable{

    private static final ObjectMapper objectMapper= JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = 1930473675796314555L;
    /**
     *
     */
    private Long id;
    /**
     *'单据编号'
     */
    private String number;
    /**
     *'养殖场id'
     */
    private Long breedFactoryId;
    /**
     *'养殖场名称'
     */
    private String breedFactoryName;
    /**
     * 购买人
     */
    private String operator;
    /**
     *'购入日期、进场日期'
     */
    private Date date;
    /**
     *'采购名称'
     */
    private String productName;
    /**
     * 兽药规格
     */
    private String standard;
    /**
     * 批准文号
     */
    private String approvalNumber;
    /**
     *'数量'
     */
    private Integer quantity;
    /**
     *'单价'
     */
    private Float price;
    /**
     *'总价'
     */
    private Float totalPrice;
    /**
     * 供应商兽药经营许可证号
     */
    private String dealerNumber;
    /**
     *'经销商名称'
     */
    private String dealerName;

    /**
     *'生产厂家、种畜场名称'
     */
    private String manufacturer;
    /**
     * 有效日期
     */
    private Date effectiveDate;
    /**
     * 批号
     */
    private String batch;
    /**
     *'药物照片'
     */
    @JsonIgnore
    private String drugPhoto;
    /**
     * 兽药照片-----不存入数据库
     */
    @Getter
    private Map<String,Map<String,String>> drug;
    /**
     *'状态，0:未提交 1：生效 '
     */
    private  Integer status;
    /**
     * 备注
     */
    private String remark;
    /**
     *'1：正常，-1：删除'
     */
    private Integer deleteFlag;
    /**
     *
     */
    private Date createdAt;
    private Date updatedAt;

    public void setDrugPhoto(String drugPhoto){
        this.drugPhoto = drugPhoto;
        if(Strings.isNullOrEmpty(drugPhoto)){
            this.drug= Collections.emptyMap();
        } else{
            try {
                this.drug = objectMapper.readValue(drugPhoto, new TypeReference<Map<String,Map<String,String>>>(){});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }
    public void setDrug(Map<String,Map<String,String>> drug) {
        this.drug = drug;
        if(drug ==null ||drug.isEmpty()){
            this.drugPhoto = null;
        }else{
            try {
                this.drugPhoto = objectMapper.writeValueAsString(drug);
            } catch (Exception e) {
                //this is fuck exception
            }
        }
    }

}

